package controllers.manage.order;

import build.OrderBuilder;
import build.OrderItemBuilder;
import com.pingplusplus.model.Charge;
import controllers.BaseController;
import controllers.auth.Secure;
import controllers.auth.SkipAuth;
import enums.GoodsType;
import enums.OrderStatus;
import enums.OrderType;
import enums.PaymentType;
import goods.Goods;
import helper.CSVUtils;
import helper.GenerateQrCodeUtil;
import helper.GlobalConfig;
import helper.PingppHelper;
import jodd.util.StringUtil;
import me.chanjar.weixin.common.util.StringUtils;
import models.AdminSupplier;
import models.admin.AdminUser;
import models.constants.DeletedStatus;
import models.merchant.Colocation;
import models.merchant.Merchant;
import models.venue.coach.Coach;
import models.venue.curriculum.Curriculum;
import models.venue.seat.Seat;
import models.weixin.WebUser;
import models.weixin.amount.RechargeCombo;
import models.weixin.amount.WebUserAccount;
import notice.OrderNotice;
import order.Order;
import order.OrderItem;
import order.OrderSource;
import play.Logger;
import play.db.jpa.JPA;
import play.libs.WS;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;
import util.common.ConvertUtil;
import util.common.DateUtil;

import java.io.File;
import java.text.DecimalFormat;
import java.util.*;


/**
 * 订单管理
 * Created by buhaoba on 2017/4/12.
 */
public class OrderQrCtrl extends Controller {


    public static void showcode() {
        render();
    }

    public static void showQRcode() {
        Map<String , Object> resultMap = new HashMap<>();
        Order order = Order.find("deleted = ? and showCode = ? and status = ?" , DeletedStatus.UN_DELETED , Boolean.TRUE , OrderStatus.UNPAID).first();
//        Logger.info("showQRcode : %s" , order );
        if(order == null) {
            resultMap.put("success" , false);
            resultMap.put("msg" , "订单不存在或已删除");
        } else {
            resultMap.put("success" , true);
            resultMap.put("id" , order.id);
            resultMap.put("orderNumber" , order.orderNumber);
            Logger.info("order.paymentType" + order.paymentType);
            if (order.paymentType == PaymentType.ALI_QR) {
                if (StringUtil.isBlank(order.aliRqcode)) {
                    Charge charge = PingppHelper.createScanCodePay(order);
                    Map<String, Object> credentialMap = charge.getCredential();
                    Logger.info("ALi_QR_CODE : %s" , credentialMap.get("alipay_qr"));
                    order.aliRqcode = credentialMap.get("alipay_qr").toString();
                    order.save();
                }
                resultMap.put("qrcode", order.aliRqcode);
            }
            if (order.paymentType == PaymentType.WEIXIN_PUB_QR) {
                if (StringUtil.isBlank(order.wxRqcode)) {
                    Charge charge = PingppHelper.createScanCodePay(order);
                    Map<String, Object> credentialMap = charge.getCredential();
                    Logger.info("WX_QR_CODE : %s" , credentialMap.get("wx_pub_qr"));
                    order.wxRqcode = credentialMap.get("wx_pub_qr").toString();
                    order.save();
                }
                resultMap.put("qrcode", order.wxRqcode);
            }
        }
//        Logger.info("QRcode" , resultMap.get("qrcode"));
        renderJSON(resultMap);
    }


    public static void checkPay(String orderNumber) {
        Map<String , Object> resultMap = new HashMap<>();
        resultMap.put("success" , false);
        Order order = Order.findByOrderNumber(orderNumber);
        if(order != null && order.status == OrderStatus.PAID) {
            resultMap.put("success" , true);
            resultMap.put("status" , order.status);
        }
        renderJSON(resultMap);
    }


}
